import { IncomingMessage } from "http";
import { WebSocket } from "ws";
import { MessageData } from "../types/MessageData";
import { decodeToken } from "../../../helpers/token-decoder";
import { conversionMessage } from "./message-converter";
import { pingHandler } from "../handlers/ping-handler";
import { selectRoomListHandler } from "../handlers/select-room-list-handler";
import { createRoomHandler } from "../handlers/create-room-handler";
import { joinRoomHandler } from "../handlers/join-room-handler";
import { leaveRoomHandler } from "../handlers/leave-room-handler";
import { startGameHandler } from "../handlers/start-game-handler";
import { setHerosHandler } from "../handlers/set-heros-handler";
import { getRoomStateHandler } from "../handlers/get-room-state-handler";
import { getDiscardCardsHandler } from "../handlers/get-discard-cards-handler";
import { getDrawCardsHandler } from "../handlers/get-draw-cards-handler";
import { getPlayerCardsHandler } from "../handlers/get-player-cards-handler";
import { setFlippedHandler } from "../handlers/set-flipped-handler";
import { setHealthHandler } from "../handlers/set-health-handler";
import { setHealthLimitHandler } from "../handlers/set-health-limit-handler";
import { setHiddenHerosHandler } from "../handlers/set-hidden-heros-handler";
import { setSkillsHandler } from "../handlers/set-skills-handler";
import { setTiedHandler } from "../handlers/set-tied-handler";
import { transferPlayerCardsToDiscardCardsHandler } from "../handlers/transfer-player-cards-to-discard-cards-handler";
import { transferPlayerCardsToDrawCardsHandler } from "../handlers/transfer-player-cards-to-draw-cards-handler";
import { transferPlayerCardsToPlayerHandCardsHandler } from "../handlers/transfer-player-cards-to-player-hand-cards-handler";
import { viewDiscardCardsHandler } from "../handlers/view-discard-cards-handler";
import { viewDrawCardsHandler } from "../handlers/view-draw-cards-handler";
import { viewPlayerCardsHandler } from "../handlers/view-player-cards-handler";
import { transferDrawCardsToDrawCardsHandler } from "../handlers/transfer-draw-cards-to-draw-cards-handler";
import { transferDrawCardsToDiscardCardsHandler } from "../handlers/transfer-draw-cards-to-discard-cards-handler";
import { transferDiscardCardsToDrawCardsHandler } from "../handlers/transfer-discard-cards-to-draw-cards-handler";
import { transferDiscardCardsToDiscardCardsHandler } from "../handlers/transfer-discard-cards-to-discard-cards-handler";
import { shuffleHandler } from "../handlers/shuffle-handler";
import { addDelayedCardsHandler } from "../handlers/add-delayed-cards-handler";
import { addMaterielCardsHandler } from "../handlers/add-materiel-cards-handler";
import { speakHandler } from "../handlers/speak-handler";
import { transferDelayedCardsToPlayerDelayedCardsHandler } from "../handlers/transfer-delayed-cards-to-player-delayed-cards-handler";
import setDeadHandler from "../handlers/set-dead-handler";
import addBuckleCardsHandler from "../handlers/add-buckle-cards-handler";

export const connectionHandler = (client: WebSocket, request: IncomingMessage) => {
	try {
		const url = new URL(request.url || "", "ws://localhost");
		const user = decodeToken(url.searchParams.get("token") || "");
		if (!user) throw new Error();
		client.onmessage = (message) => {
			const data = JSON.parse(message.data as string) as MessageData;
			switch (data.type) {
				case "ping":
					pingHandler({ client, user, payload: data.payload });
					break;
				case "createRoom":
					createRoomHandler({ client, user, payload: data.payload });
					break;
				case "speak":
					speakHandler({ client, user, payload: data.payload });
					break;
				case "getDiscardCards":
					getDiscardCardsHandler({ client, user, payload: data.payload });
					break;
				case "getDrawCards":
					getDrawCardsHandler({ client, user, payload: data.payload });
					break;
				case "getPlayerCards":
					getPlayerCardsHandler({ client, user, payload: data.payload });
					break;
				case "getRoomState":
					getRoomStateHandler({ client, user, payload: data.payload });
					break;
				case "joinRoom":
					joinRoomHandler({ client, user, payload: data.payload });
					break;
				case "leaveRoom":
					leaveRoomHandler({ client, user, payload: data.payload });
					break;
				case "selectRoomList":
					selectRoomListHandler({ client, user, payload: data.payload });
					break;
				case "shuffle":
					shuffleHandler({ client, user, payload: data.payload });
					break;
				case "setFlipped":
					setFlippedHandler({ client, user, payload: data.payload });
					break;
				case "setHealth":
					setHealthHandler({ client, user, payload: data.payload });
					break;
				case "setHealthLimit":
					setHealthLimitHandler({ client, user, payload: data.payload });
					break;
				case "setHeros":
					setHerosHandler({ client, user, payload: data.payload });
					break;
				case "setHiddenHeros":
					setHiddenHerosHandler({ client, user, payload: data.payload });
					break;
				case "setSkills":
					setSkillsHandler({ client, user, payload: data.payload });
					break;
				case "setTied":
					setTiedHandler({ client, user, payload: data.payload });
					break;
				case "startGame":
					startGameHandler({ client, user, payload: data.payload });
					break;
				case "transferDrawCardsToDrawCards":
					transferDrawCardsToDrawCardsHandler({ client, user, payload: data.payload });
					break;
				case "transferDrawCardsToDiscardCards":
					transferDrawCardsToDiscardCardsHandler({ client, user, payload: data.payload });
					break;
				case "transferDiscardCardsToDrawCards":
					transferDiscardCardsToDrawCardsHandler({ client, user, payload: data.payload });
					break;
				case "transferDiscardCardsToDiscardCards":
					transferDiscardCardsToDiscardCardsHandler({ client, user, payload: data.payload });
					break;
				case "transferPlayerCardsToDiscardCards":
					transferPlayerCardsToDiscardCardsHandler({ client, user, payload: data.payload });
					break;
				case "transferPlayerCardsToDrawCards":
					transferPlayerCardsToDrawCardsHandler({ client, user, payload: data.payload });
					break;
				case "transferPlayerCardsToPlayerHandCards":
					transferPlayerCardsToPlayerHandCardsHandler({ client, user, payload: data.payload });
					break;
				case "transferDelayedCardsToPlayerDelayedCards":
					transferDelayedCardsToPlayerDelayedCardsHandler({ client, user, payload: data.payload });
					break;
				case "viewDiscardCards":
					viewDiscardCardsHandler({ client, user, payload: data.payload });
					break;
				case "viewDrawCards":
					viewDrawCardsHandler({ client, user, payload: data.payload });
					break;
				case "viewPlayerCards":
					viewPlayerCardsHandler({ client, user, payload: data.payload });
					break;
				case "addMaterielCards":
					addMaterielCardsHandler({ client, user, payload: data.payload });
					break;
				case "addDelayedCards":
					addDelayedCardsHandler({ client, user, payload: data.payload });
					break;
				case "addBuckleCards":
					addBuckleCardsHandler({ client, user, payload: data.payload });
					break;
				case "setDead":
					setDeadHandler({ client, user, payload: data.payload });
					break;
				default:
					client.send(conversionMessage({ type: "notFound", payload: null }));
			}
		};
	} catch (error) {
		console.error(error);
		client.close();
	}
};
